
import {ref,reactive,computed} from 'vue'
import Exam from '@/types/exam'
import request from '@/base/request'
import Page from '@/base/page'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
export default function(){
    interface ExamInfo extends Exam{

    }


    interface CreatRandExamParm{
        examName:string,
        beginTime:string,
        validity:number,
        score:number
        examScoreList:Array<number>,
        examNumberList:Array<number>
    }

    const creatRandExamParm=ref<CreatRandExamParm>({
        examName:"",
        beginTime:new Date().toLocaleString(),
        validity:60,
        examScoreList:[0,1,1,1,1,1],
        examNumberList:[0,0,0,0,0,0],
        score:0
    })

    const showCreateByRand = ref(false)

    const totalscore=ref(computed(()=>{
        let res=0;
        for(let i=1;i<6;i++){
            res+=creatRandExamParm.value.examNumberList[i]*creatRandExamParm.value.examScoreList[i]
        }
        return res;
    }))


    const examList=ref<Page<ExamInfo>>({
        page:1,
        pageSize:9,
        counts:0,
        items:[]
    })
    const examParm=ref({
        pageNo:1,
        pageSize:9,
        id:"",
        examName:"",
        classBelongId:"",
    })

    const ruleFormRef=ref()

    const typeNumberList=ref<Array<number>>([0,0,0,0,0,0,0]);


    async function getExamList() {

        const response=(await request.post("/problem/exam/admin/getExamList",examParm.value)).data
        if(response.code===200){
            examList.value=response.data
        }else{
            ElMessage(`查询考试错误:${response.msg}`)
        }
    }


    async function delExam(id:number) {
        const response = (await request.delete(`/problem/exam/admin/del/${id}`)).data
        if(response.code===200){
            ElMessage.success("删除成功")
            getExamList();
        }else{
            ElMessage.error(`失败${response.msg}`)
        }
    }

    async function getTypeNumberList() {
        const response = (await request.get("/problem/admin/getTypeNumberList")).data
        if(response.code===200){
            typeNumberList.value=response.data
        }else{
            ElMessage.error(`获取题目类型数量失败${response.msg}`)
        }
    }

    async function createExamByRandom() {
        if(!ruleFormRef.value)return

        ruleFormRef.value.validate(async (valid)=>{
            if(valid){
                creatRandExamParm.value.score=totalscore.value
                creatRandExamParm.value.beginTime=new Date(creatRandExamParm.value.beginTime).toLocaleString()
                const response=(await (request.post("/problem/exam/admin/creatExamByRandom",creatRandExamParm.value))).data
                if(response.code===200){
                    ElMessage.success(`创建成功，请及时编辑考试进行数据校验,id:${response.data}`)
                    showCreateByRand.value=false
                    getExamList()
                }else{
                    ElMessage.error(`失败${response.msg}`)
                }
            }
        })

    }


    const rules = reactive<FormRules<CreatRandExamParm>>({
        examName: [
            { required: true, message: '请输入考试名', trigger: 'blur' },
            { min: 1, max: 30, message: '请在1到30字之间', trigger: 'blur' },
        ],
        validity: [
            { required: true, message: '请输入一个数字', trigger: 'blur' },
            { type: 'number', message: '请输入一个数字', trigger: ['blur', 'change'] },
            {
                validator: (rule, value, callback) => {
                    if (value <= 1 || value>=10000000) {
                        callback(new Error('必须大于1小于10000000'));
                    } else {
                        callback();
                    }
                }, trigger: ['blur', 'change']
            },
        ],
        beginTime:[
            { required: true, message: '请选择时间', trigger: 'blur' },
        ]
    })

    function handlePageChange(newPage:number){
        examParm.value.pageNo=newPage
        getExamList()
    }





    return {creatRandExamParm,showCreateByRand,totalscore,examList,ruleFormRef,examParm,rules,typeNumberList,createExamByRandom,getTypeNumberList,delExam,getExamList,handlePageChange}
}